package ysu.sxm.ptjc.Utill;

import ysu.sxm.ptjc.Domin.DateRange;

import java.util.*;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;

public class Utills {
    //设置年月格式
    public static String DEFAULT_FORMAT = "yyyy-MM-dd";
    /**
     * 格式化日期
     * @param date 日期对象
     * @return String 日期字符串
     */
    public static String formatDate(Date date){
        SimpleDateFormat f = new SimpleDateFormat(DEFAULT_FORMAT);
        String sDate = f.format(date);
        return sDate;
    }

    //返回月份日期范围
    public static DateRange getMonthDays(Integer month){
        String start="";
        String end="";
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR,calendar.get(Calendar.YEAR));
        calendar.set(Calendar.MONTH,month-1);
        calendar.set(Calendar.DAY_OF_MONTH,calendar.getMinimum(Calendar.DATE));
        start=formatDate(calendar.getTime());
        calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DATE));
        end=formatDate(calendar.getTime());
        return new DateRange(start,end);
    }
    public static List<DateRange> getMonthDays(List<Integer> months){
        List<DateRange> dateRanges=new ArrayList<>();
        for (Integer month: months
             ) {
            dateRanges.add(getMonthDays(month));
        }
        return dateRanges;
    }
    //返回周日期范围
    public static List<DateRange> getWeekDays(List<Integer> weeks){
        List<DateRange> dateRanges=new ArrayList<>();
        String start="";
        String end="";
        for (Integer week:weeks
             ) {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
            calendar.set(Calendar.WEEK_OF_YEAR,week);
            start=formatDate(calendar.getTime());
            calendar.add(Calendar.DATE,7);
            calendar.set(Calendar.WEEK_OF_YEAR,week+1);
            calendar.roll(Calendar.DATE,false);
            end=formatDate(calendar.getTime());
            dateRanges.add(new DateRange(start,end));
        }
        return dateRanges;
    }
    /**
     *     输入数据格式化
     *     为了模拟数据的输入
     */
    public static String formatData(int data,boolean isCustomer){
        String res="";
        if(!isCustomer){
           res=String.format("%02d",data);
        }else{
            res=String.format("%05d",data);
        }
        return res;
    }

    //计算缺陷率并保留两位小数
    public  static String calculation(long single,long all){
        DecimalFormat df = new DecimalFormat("0.00");
        if(all==0){
            return df.format(0);
        }

        return df.format((single/(double)all)*100);
    }

    /**
     * 处理top数据返回
     */
    public static List<Map.Entry<String, String>> topRes(Map<String,String> res,int value){
        List<Map.Entry<String, String>> list = new ArrayList<>(res.entrySet());
        if(res==null||res.isEmpty()){
            return list;
        }
        Collections.sort(list, (o1,o2)->{
            return Double.valueOf(o2.getValue()).compareTo(Double.valueOf(o1.getValue()));
        });
        if (list.size() > value) {
            for (int i = value - 1; i < list.size() - 1; i++) {
                if (!list.get(i + 1).getValue().equals(list.get(i).getValue())) {
                    list = list.subList(0, i + 1);
                }
            }
        }
        return list;
    }
    public static void main(String[] args) {
        List<Integer> years=new ArrayList<>();
        years.add(1);
        years.add(2);
        years.add(4);
        years.add(30);
        List<DateRange> dateRanges=getWeekDays(years);
        for (DateRange d:dateRanges
             ) {
            System.out.println(d.toString());
        }
    }
}
